<?php

class Action
{
    public function index() {

        $title_column = $data = $from = $to = $search_filter = null;

        extract(URLTracker::init(array(
            'search_filter' => array(
                'agency_id' => 0,
                'start_date' => $from,
                'end_date' => $to,
                'show_goods' => 0,
            ),
        )));

        $agency_id = $search_filter['agency_id'];
        $show_goods = $search_filter['show_goods'];

        $AGENCY_LIST = user_helper::get_all_dealers_info();

        // 支付时间
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $from = $search_filter['start_date'];
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $to = $search_filter['end_date'];
        }

        if (!$from || !$to) {
            $from = $to = date('Y-m-d', strtotime('-1 day'));
            URLTracker::parse(array(
                'search_filter' => array(
                    'start_date' => $from,
                    'end_date' => $to
                )
            ), 1);
        }

        if ($agency_id > 0) {

            $filter = ' WHERE 1' .
                      ($from ? ' AND m.`date` >= \'' . $from . '\'' : '') .
                      ($to ? ' AND m.`date` <= \'' . $to . '\'' : '') .
                      ' AND m.`agency_id`=' .
                      $agency_id;

            // 地区出货,销售额汇总
            $SQL = 'SELECT ' . join(',', array(
                    'm.`date`',
                    'm.`order_id`',
                    'm.`category_id`',
                    'm.`goods_id`',
                    'SUM(m.`real_price`) sum_price',
                    'SUM(m.`goods_num`) sum_num',
                )) . ' FROM `metadata_dealer_order_goods_sku_daily` m' . $filter . ' GROUP BY m.`date`, m.`goods_id`';

            $sums = run_sql('metadata_dealer_order_goods_sku_daily', 'getAll', $SQL);

            $tree = goods_helper::get_category_goods_tree();
            $category_list = goods_config::goods_category();
            $goods_list = goods_config::goods_list();

            $title_column = $index = array();
            foreach ($tree as $category_id => $good_list) {
                $index[$category_id] = array(0, '0.00', array());
                $title_column[$category_id] = array($category_list[$category_id]['title'], array());
                if (!!$show_goods) {
                    foreach ($good_list as $goods_id) {
                        $index[$category_id][2][$goods_id] = array(0, '0.00');
                        $title_column[$category_id][1][$goods_id] = $goods_list[$goods_id]['title'];
                    }
                }
            }

            $data = array();
            foreach ($sums as $item) {
                if (!isset($data[$item['date']])) {
                    $data[$item['date']] = array();
                }
                $data[$item['date']][] = $item;
            }

            foreach ($data as $date => $dd) {
                $count_order_id = count(array_unique(array_column($dd, 'order_id')));

                $res = $index;
                for ($i = 0, $len = count($dd); $i < $len; $i++) {
                    $item = $dd[$i];
                    $res[$item['category_id']][0] += $item['sum_num'];
                    $res[$item['category_id']][1] += $item['sum_price'];

                    if (!!$show_goods) {
                        $res[$item['category_id']][2][$item['goods_id']][0] = $item['sum_num'];
                        $res[$item['category_id']][2][$item['goods_id']][1] = $item['sum_price'];
                    }
                }

                foreach ($res as $k => $v) {
                    $res[$k][1] = num_keep_two_round($v[1]);
                }

                $data[$date] = array_merge(array('count_order_id' => $count_order_id), $res);
            }

            $title_column = array_merge(array('count_order_id' => '订单数'), $title_column);

            krsort($data);

        }

        Response::assign('title_column', $title_column);
        Response::assign('data', $data);
        Response::assign('AGENCY_LIST', $AGENCY_LIST);
        Response::display('admin/agency.html');
    }
}